layui.config({
    base: "/infomssh/js/"
})
layui.use(['form', 'layer', 'laydate', 'table', 'upload', 'jquery_cookie'], function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        upload = layui.upload,
        table = layui.table;
    var cookie = layui.jquery_cookie();

    //年月范围选择
    laydate.render({
        elem: '#startTime',
        type: 'month',
        value: new Date()
    });
    laydate.render({
        elem: '#endTime',
        type: 'month',
        value: new Date()
    });

    var tableIns = table.render({
        elem: '#cashList',
        url: '/studentCash/getAllStudentCash',
        where: { startTime: new Date().toDateString(), endTime: new Date().toDateString(), studentNo: $('.studentNo').val() },
        headers: { 'Authorization': "Bearer " + cookie.cookie("token") },
        page: true,
        cellMinWidth: 95,
        height: "full-104",
        limit: 20,
        limits: [10, 20, 30, 40, 50, 100, 200],
        id: "cashListTab",
        cols: [
            [
                { type: "checkbox", fixed: "left", width: 50 },
                {
                    field: 'StudentNo',
                    title: '学号',
                    width: 100,
                    align: "center",
                },
                { field: 'Name', title: '姓名', minWidth: 120 },
                {
                    field: 'Amount',
                    title: '金额',
                    width: 180,
                    align: "center",
                },
                {
                    field: 'Bz',
                    title: '备注',
                    minWidth: 100,
                    align: 'center',
                },
                {
                    field: 'StudentGrade',
                    title: '年级',
                    minWidth: 100,
                    align: 'center',
                },
                {
                    field: 'YearMonth',
                    title: '年月',
                    minWidth: 100,
                    align: 'center',
                },
                {
                    field: 'AmountCategory',
                    title: '费用类型',
                    minWidth: 100,
                    align: 'center',
                },
                {
                    title: '操作',
                    width: 130,
                    fixed: "right",
                    align: "center",
                    templet: function() {
                        return '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a><a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
                    }
                }
            ]
        ]
    });

    $(".search_btn").on("click", function() {
        table.reload("cashListTab", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                startTime: $('#startTime').val(),
                endTime: $('#endTime').val(),
                studentNo: $('.studentNo').val()
            }
        })

    });
    $('.uploadFile_btn').on('click', function() {
        var index = layui.layer.open({
            title: "上传文件",
            type: 2,
            area: ["300px", "385px"],
            content: "studentCashUpload.html",
            success: function(layero, index) {
                setTimeout(function() {
                    layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
        layui.layer.full(index);
    })
    $('.Delete_btn').on('click', function() {
        var checkStatus = table.checkStatus('cashListTab'),
            data = checkStatus.data,
            newsId = [];
        if (data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].Id);
            }
            layer.confirm('确定删除选中的数据？', { icon: 3, title: '提示信息' }, function(index) {
                $.ajax({
                    type: "Post",
                    url: '/studentCash/deleteStudentCashBatch',
                    headers: { 'Authorization': "Bearer " + cookie.cookie("token") },
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify({ ids: newsId }), //用ajax时必须序列化，不能直接用对象。
                    success: function(strdata) {
                        var data;
                        if (typeof(strdata) == "string") {
                            var data = JSON.parse(strdata); //部分用户解析出来的是字符串，转换一下
                        } else {
                            data = strdata;
                        }
                        if (data.Code == 200) {
                            top.layer.msg("数据删除成功！");

                            table.reload("cashListTab", {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                },
                                where: {
                                    startTime: $('#startTime').val(),
                                    endTime: $('#endTime').val(),
                                    studentNo: $('.studentNo').val()
                                }
                            })
                        } else {
                            layer.msg(data.Message);
                        }
                        layer.close(index);
                    }
                });
            })
        } else {
            layer.msg("请选择需要删除的数据");
        }
    })
    $('.downLoadFile_btn').on('click', function() {
        layer.confirm('确定下载时间范围内的统计？', { icon: 3, title: '提示信息' }, function(index) {
            $("#downFileIframe").attr('src', '/studentCash/downLoadFile?startTime=' + $('#startTime').val() + '&&endTime=' + $('#endTime').val())
            layer.close(index);
        })
    })

})